function getFileName( id )
{
    var fileName = id.slice( 16 );
    return fileName;
}

function changeRatingHandler( response, fileName )
{
    if ( response == 'already used' )
    {
        alert( 'You are already vote!' );
    }
    else if ( response == 'error' )
    {
        alert( 'Sorry! Error in rating system.' );
    }
    else
    {
        var container = document.getElementById( 'rating_' + fileName );
        if ( response )
        {
            container.textContent = response;
        }
    }
}

function changeRating()
{
    var fileName = getFileName( this.id );
    var param = '?file_name=' + fileName + '&change_rating=';
    if ( this.textContent == '+')
    {
        param += 'plus';
    }
    else
    {
        param += 'minus';
    }    
    var xmlHttp = getXmlHttp();
    xmlHttp.open( 'GET', '/php/ajax/file_rating.php' + param, true );
    xmlHttp.onreadystatechange = function()
    {
        changeRatingHandler( xmlHttp.responseText, fileName );
    }
    xmlHttp.send( null );
}

function initializeRatingButtons()
{
    var ratingButtons = document.getElementsByTagName( 'a' );
    for ( var i = 0; i < ratingButtons.length; ++i )
    {
        if ( ratingButtons[i].id.match( /^[pm]{1}_rating_button_/ ) )
        {
            ratingButtons[i].onclick = changeRating;
        }
    }
}